package com.tuanzi.loan.schedule.receipt;


import com.tuanzi.loan.business.entity.extend.ExtendInfo;
import com.tuanzi.loan.business.entity.receipt.statistics.ReceiptExtendStatistics;
import com.tuanzi.loan.business.service.receipt.statistics.ReceiptExtendStatisticsService;
import com.tuanzi.loan.core.aspect.Debug;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Debug
@Slf4j
@Service
@Transactional
public class StatisticsReceiptExtendScheduler {


    @Autowired
    private ReceiptExtendStatisticsService receiptExtendStatisticsService;



//    @Scheduled(cron = "${cron.statistics.receipt-extend}")
    public void insertExtendStatistics() {
        DateTime nowTime = new DateTime(new Date()).plusDays(-1);
        Date startDate = new DateTime(nowTime.getYear(), nowTime.getMonthOfYear(), nowTime.getDayOfMonth(), 0, 0, 0).toDate();
        Date endDate = new DateTime(nowTime.getYear(),nowTime.getMonthOfYear(),nowTime.getDayOfMonth(),23,59,59).toDate();

        List<ExtendInfo> allExtendInfos = receiptExtendStatisticsService.findAllExtendInfos();
        List<ReceiptExtendStatistics> extendStatisticsList = new ArrayList<>();
        NumberFormat numberFormat= NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(2);
        for (ExtendInfo extendInfo : allExtendInfos) {
            ReceiptExtendStatistics statistics = new ReceiptExtendStatistics();
            statistics.setDate(startDate);
            statistics.setProductId(extendInfo.getProductId());
            statistics.setProductName(extendInfo.getProductName());
            statistics.setChannelId(extendInfo.getId());
            statistics.setChannelName(extendInfo.getChannelName());
            statistics.setChannelLink(extendInfo.getExtendUrl());

            //访问连接UV
            statistics.setLinkUV(receiptExtendStatisticsService.getNginxUV(extendInfo.getExtendUrl(),startDate));
            //用户注册数
            statistics.setRegisterCount(receiptExtendStatisticsService.getRegisterCount(extendInfo.getId(),startDate,endDate));

            //访问->申请转化率
            if (statistics.getLinkUV() == 0) {
                statistics.setRate("0%");
            } else {
                statistics.setRate(numberFormat.format((float) statistics.getApplyCount() / (float) statistics.getLinkUV() * 100) + "%");
            }

            //申请数,根据产品id，外推渠道id和时间查询订单数
            Integer applyCount = receiptExtendStatisticsService.findOrderCountByProdIdAndChanelIdAndDate(extendInfo.getProductId(), extendInfo.getId(), startDate, endDate);
            statistics.setApplyCount(applyCount);
            extendStatisticsList.add(statistics);
        }
        receiptExtendStatisticsService.saveOrUpdate(extendStatisticsList);
    }


}
